﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace UIWPF
{
    /// <summary>
    /// Interaction logic for TestWindow.xaml
    /// </summary>
    public partial class TestWindow : Window
    {
        private delegate void Effecting();
        private int timeWait = 10;

        private void LastingEffecting()
        {
            byte redStep = 1, red = 1;
            byte greenStep = 1, green = 1;
            byte blueStep = 1, blue = 1;

            while (true)
            {
                for (; red >=0 && red <= 255; red += redStep)
                {
                    ChangeColor(Color.FromArgb(byte.MaxValue, red, green, blue));
                }
                for (; green >= 0 && green <= 255; green += greenStep)
                {
                    ChangeColor(Color.FromArgb(byte.MaxValue, red, green, blue));
                }
                for (; blue >= 0 && blue <= 255; blue += blueStep)
                {
                    ChangeColor(Color.FromArgb(byte.MaxValue, red, green, blue));
                }
                redStep = (byte) -redStep;
                greenStep = (byte) -greenStep;
                blueStep = (byte) -blueStep;
            }
        }

        private void OpenEffecting()
        {
            while (true)
            {
                this.Opacity += 0.02;
                if (this.Opacity >= 1.0)
                {
                    break;
                }
                Thread.Sleep(timeWait);
            }
//            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Effecting(LastingEffecting));
        }

        private void CloseEffecting()
        {
            while (true)
            {
                this.Opacity -= 0.02;
                if (this.Opacity <= 1.0)
                {
                    Hide();
                    break;
                }
                Thread.Sleep(timeWait);
            }
        }

        public TestWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            ChangeColor(Color.FromArgb((byte)a.Value, (byte)r.Value, (byte)g.Value, (byte)b.Value));
        }

        public void ChangeColor(Color c)
        {
            this.Resources.Remove("color");
            this.Resources.Add("color", new SolidColorBrush(c));
        }

        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void FormLoaded(object sender, RoutedEventArgs e)
        {
            this.Opacity = 0.0;
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Effecting(OpenEffecting));
        }
    }
}
